<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<link rel="stylesheet" href="/layui/css/layui.css">
<script src="/layui/layui.js"></script>
<style type="text/css">
	.layui-table-tips-main{display:none}
	.layui-table-tips-c{display:none}
</style>
</head>
<body class="layui-layout-body">
	<div class="layui-form"  style="margin-top: 20px;">
		<div class="layui-form toolbar">
			<div class="layui-form-item">
				<div class="layui-inline">
					<div class="layui-inline">
						<div class="layui-input-inline mr0" style="margin-left: 10px;">
							<input id="authEdtSearch" class="layui-input" type="text" autocomplete="off" placeholder="输入查询关键字">
						</div>
					</div>
					<div class="layui-inline">
						<div class="layui-input-inline mr0">
							<select name="isstatus" lay-filter="isstatus"  lay-search  id="isstatus">
								<option value="">目录是否已上线</option>
								<option value="1">已上线</option>
								<option value="0">未上线</option>
							</select>
						</div>
					</div>
					<div class="layui-inline">
						<button id="authBtnSearch" class="layui-btn icon-btn layui-btn-normal"><i class="layui-icon"></i>搜索</button>
						<button id="addFirstMenu" class="layui-btn icon-btn layui-btn-normal"><i class="layui-icon"></i>添加一级菜单</button>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!--  数据data -->
	<div class="layui-row" style="padding: 10px 10px;" id="tablediv">
		<table id="table_data" lay-filter="test"  ></table>	
	</div>
	<!-- 表格操作列 -->
	<script type="text/html" id="authTableBar">
		{{# if (d.ctier!=3) { }}  
			<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="pass">添加下一层</a>
		{{# } }} 
  		<a class="layui-btn layui-btn-danger layui-btn-xs"  lay-event="del">删除</a>
	</script>
	<script type="text/html" id="tbaleState">
    	<input type="checkbox" lay-filter="ckState" value="{{d.cid}}" lay-skin="switch" lay-text="已上线|未上线"
           	{{d.ispopup==1?'checked':''}}/>
	</script>
	<script type="text/javascript">
		$(top.hangge());	
	</script>
	<script type="text/javascript">
	var tableIns ;
	var layer ;
	layui.use(['layer', 'form', 'table','element'], function(){
		  var table = layui.table;
		  var form = layui.form;
		  var element = layui.element;
		  layer=layui.layer;
		  
		  //加载层
		  var loadingIndex =layer.msg('数据加载中...', {
			   icon: 16
			  ,shade: 0.01
		  });  
		  
		  //第一个实例
		  var params = {};
		  tableIns = table.render({
		    elem: '#table_data',
		    url: '/admin/knowledge/findListData6',
		    page: true, //开启分页
		    where:params,
		    loading:true,
		    height: 'full-150',
		    cols: [
		    	[ //表头
		    	{field:'cid',title: '编号', hide: true}
               , {field: 'cno', title: '菜单编号'}
               , {field: 'cname', title: '菜单名称',edit: 'text'}
               , {field: 'ctop', title: '上一级编号'}
               , {field: 'cfinish', title: '是否为最后一级',templet:function(c){
            	   if(c.cfinish) return '是';
            	   return '否';
               }}
               , {field: 'cpath', title: '对应的文章编号'}
               , {field: 'ctier', title: '所处层数',templet:function(c){
            	   return '第'+c.ctier+'层'
               }}
               , {field: 'ispopup',templet: '#tbaleState', title: '是否上线',width:120}
		       , {templet: '#authTableBar', width: 160, align: 'center', title: '操作'}
		    	]
		   		],
		    done:function(res){
		    	layer.close(loadingIndex);
		    }
		  });
		  
		  table.on('edit(test)', function(obj){
			    var value = obj.value //得到修改后的值
			    ,data = obj.data //得到所在行所有键值
			    ,field = obj.field; //得到字段
			    var params={};
				params.cid=data.cid;
				params.cname=value;
			    $.ajax( {
			         url : '/admin/knowledge/modification6', 
			         type : 'post',
			         data: params,
			         dataType : 'json',
			         success : function(data) {
			        	 if(data.code==0){
			        		 layer.msg(data.msg,{icon: 1});
			        	 }else{
			        		 layer.msg(data.msg,{icon: 2});
			        	 }
			         }
			     });
			    //layer.msg('[ID: '+ data.cid +'] ' + field + ' 字段更改为：'+ value);
			  });
		  
		  //数据操作
		  table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
			    var data = obj.data //获得当前行数据
			    ,layEvent = obj.event; //获得 lay-event 对应的值
			    var cid=data.cid;
			    var cno=data.cno;
			    let ctier = data.ctier;
			    if(layEvent === 'del'){
			    	layer.confirm('确认要删除选中的行？', function(index){
			        obj.del(); //删除对应行（tr）的DOM结构
			        layer.close(index);
			        //向服务端发送删除指令
			        del(cid,cno);
			      });
			    } else if(layEvent === 'pass'){
			    	layer.open({
						  title:'新增菜单信息',
						  content: '/admin/knowledge/goAdd6?ctier='+ctier+'&handSn='+cno,
						  type: 2, 
						  area: ['25%', '50%'],
						  scrollbar: true,
						  cancel: function(index, layero){ 
						  } ,
						  end:function(){
							  Refresh();
						  }
					}); 
			    } 
			  });
		//状态修改
	  form.on('switch(ckState)', function (obj) {
             layer.load(2);
             $.get('/admin/knowledge/updateWeekend6', {
            	 id: obj.elem.value,
             }, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1});
                } else {
                    layer.msg(data.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        }); 
	});
	
	
	//添加菜单
	$("#addFirstMenu").click(function(){
		 layer.open({
			  title:'新增菜单信息',
			  content: '/admin/knowledge/goAdd6?ctier=0&handSn=0',
			  type: 2, 
			  area: ['25%', '50%'],
			  scrollbar: true,
			  cancel: function(index, layero){ 
				  
			  } ,
			  end:function(){
				  Refresh();
			  }
		}); 
	});
	
	//搜索
	$("#authBtnSearch").click(function(){
		Refresh();
	});
	//刷新
	function Refresh(){
		var params = {};
		var authEdtSearch=$("#authEdtSearch").val();
		var ispopup=$("#isstatus").val();
		params.keywords=authEdtSearch;
		params.ispopup=ispopup;
		tableIns.reload({
			  where:params
			  ,page: {
				    curr: 1 //重新从第 1 页开始
				  }
		});	
	}
	
	//删除
	function del(cid,cno){
		var params={};
		params.cid=cid;
		params.cno=cno;
		$.ajax( {
	         url : '/admin/knowledge/delete6', 
	         type : 'post',
	         data: params,
	         dataType : 'json',
	         success : function(data) {
	        	 if(data.code==0){
	        		 layer.msg('删除成功',{icon: 1});
	        		 Refresh();
	        	 }else{
	        		 layer.msg(data.msg,{icon: 2});
	        	 }
	         }
	     });
	}
	</script>


</body>
</html>